package com.amos.generate.service.impl;

import com.amos.core.BaseDTO;
import com.amos.generate.bean.*;
import com.amos.generate.service.AbstractColumnHandler;
import com.amos.generate.type.TemplateTypeEnum;
import com.amos.generate.util.VelocityUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright © 2018 嘉源锐信. All rights reserved.
 *
 * @Project: mu-exam
 * @ClassName: DtoHandler
 * @Package: com.amos.generate.service.impl
 * @author: zhuqb
 * @Description:
 * @date: 2020/3/11 0011 下午 14:39
 * @Version: V1.0
 */
public class DtoHandler extends AbstractColumnHandler<DtoField> {

    private String path;
    private String rootPackageName;
    private String tableName;
    private boolean isPage;

    private List<DataBaseField> list;

    public DtoHandler(String path, String rootPackageName, String tableName, boolean isPage, List<DataBaseField> list) {
        super(path, rootPackageName, tableName, TemplateTypeEnum.DTO,isPage,list);
        this.path = path;
        this.rootPackageName = rootPackageName;
        this.tableName = tableName;
        this.isPage = isPage;
        this.list = list;
    }

    @Override
    protected Class<?> getFilterClass() {
        return BaseDTO.class;
    }

    @Override
    public DtoField getColumnList(List<DataBaseField> list) {
        DtoField field = new DtoField();
        List<MuField> fields = new ArrayList<>(list.size());
        List<String> imports = new ArrayList<>();
        list.stream().forEach(item->{
            MuField muField  = new MuField();
            muField.setJavaField(VelocityUtils.underline2Camel(item.getColumnName()));
            muField.setJavaType(getJavaType(item));
            muField.setColumn(item.getColumnName());
            muField.setLength(item.getCharacterMaximumLength());
            muField.setType(item.getDataType());
            muField.setRemark(item.getColumnComment());
            fields.add(muField);
            String importString = this.parseForImport(item);
            if (!StringUtils.isEmpty(importString)) {
                imports.add(importString);
            }
        });
        field.setImports(imports);
        field.setColumns(fields);
        return field;
    }

}
